package com.ftg.learn.service.impl;

import com.ftg.learn.domain.SysMenu;
import com.ftg.learn.domain.SysUserDomain;
import com.ftg.learn.mapper.SysUserMapper;
import com.ftg.learn.service.SysUserService;
import com.ftg.learn.util.MenuVo;
import com.ftg.learn.util.PageEx;
import com.ftg.learn.util.TipMessUtil;
import com.ftg.learn.util.ToWeb;
import com.ftg.learn.vo.UserListVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author kangjx
 * @date 2024/1/31
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    SysUserMapper sysUserMapper;


    @Override
    public ToWeb<PageEx<UserListVo>> list(String name, String phone, Integer status, long pageNo, long pageSize) {
        PageEx<UserListVo> ex = new PageEx<>(pageNo, pageSize);
        ex.setRows(sysUserMapper.selectUserList(name, phone, status, (pageNo - 1) * pageSize, pageSize));
        ex.setTotal(sysUserMapper.selectCount(name, phone, status));
        return ToWeb.success(ex);
    }

    @Override
    public ToWeb<Integer> insert(SysUserDomain domain, Integer[] roleids) {

        if (Objects.nonNull(sysUserMapper.findByAccount(domain.getAccount()))) {
            return ToWeb.failure("帐号已存在!!",0);
        }

        sysUserMapper.insert(domain);

        Integer userId = sysUserMapper.findUserIdByAccount(domain.getAccount());

        //用户角色批量添加
        sysUserMapper.insertUserRole(userId,roleids);

        return ToWeb.success(1);
    }

    @Override
    public ToWeb<Integer> deleteById(Integer id) {
        sysUserMapper.deleteUserRoleByUserId(id);
        int num = sysUserMapper.deleteById(id);
        return ToWeb.success(num);
    }

    @Override
    public ToWeb<Map<String, Object>> findone(Integer id) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("userInfo", sysUserMapper.findone(id));
        map.put("userRoleInfo",sysUserMapper.findRoleByUserid(id));
        return ToWeb.success(map);
    }

    @Override
    public ToWeb<Integer> update(SysUserDomain domain, Integer[] roleIds) {

        if (Objects.isNull(sysUserMapper.findone(domain.getId()))) {
            return ToWeb.failure("帐号已删除!!",0);
        }

        int num = sysUserMapper.updateUser(domain);

        //删除中间表
        sysUserMapper.deleteUserRoleByUserId(domain.getId());

        //添加中间表
        sysUserMapper.insertUserRole(domain.getId(),roleIds);

        return ToWeb.success(num);
    }

    @Override
    public ToWeb<SysUserDomain> login(String account, String password) {
        if(Objects.isNull(account)){
            return ToWeb.failure(TipMessUtil.ACCOUNT_ERROR_TIP,null);
        }

        if(Objects.isNull(password)){
            return ToWeb.failure("密码不能为空!!",null);
        }

        SysUserDomain userinfo = sysUserMapper.login(account,password);
        if(Objects.isNull(userinfo)){
            return ToWeb.failure("对不起，用户名或者密码错误!!",null);
        }else{
            if (userinfo.getStatus() == 1) {
                return ToWeb.failure("对不起，该用户已被禁用!!",null);
            }else{
                return ToWeb.success(userinfo);
            }
        }
    }

    @Override
    public ToWeb<List<MenuVo>> auth(Integer userId) {
        List<SysMenu> auth = sysUserMapper.auth(userId);
        return ToWeb.success(getRoot(auth));
    }


    /**
     * 获取根节点
     * @param sysMenus 菜单
     * @return 根节点对象
     */
    private List<MenuVo> getRoot(List<SysMenu> sysMenus){

        List<MenuVo> trees = new ArrayList<>();


        for (SysMenu sysMenu : sysMenus) {
            if (sysMenu.getParentId() == null) {
                MenuVo root = new MenuVo();
                root.setId(sysMenu.getId());
                root.setName(sysMenu.getName());
                root.setStatus(sysMenu.getStatus());
                root.setParentId(sysMenu.getParentId());
                root.setType(sysMenu.getType());
                root.setUrl(sysMenu.getUrl());
                root.setChildren(new ArrayList<>());
                trees.add(root);
                /**
                 * 递归查找子节点
                 * @param sysDeptDomains 部门列表
                 * @param root 根节点对象
                 */
                findSon(sysMenus,root);
            }
        }
        return trees;
    }



    /**
     * 递归查找子部门
     * @param menus 菜单列表
     * @param current 当前菜单
     */
    private void findSon(List<SysMenu> menus, MenuVo current){
        for (SysMenu menu : menus) {
            if (current.getId().equals(menu.getParentId())) {

                MenuVo root = new MenuVo();
                root.setId(menu.getId());
                root.setName(menu.getName());
                root.setStatus(menu.getStatus());
                root.setParentId(menu.getParentId());
                root.setType(menu.getType());
                root.setUrl(menu.getUrl());
                root.setChildren(new ArrayList<>());

                current.getChildren().add(root);

                findSon(menus, root); // 继续递归查找子部门

            }
        }
    }

}
